<?php

use yii\helpers\Inflector;
use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$urlParams = $generator->generateUrlParams();
$nameAttribute = $generator->getNameAttribute();
$model = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
    $safeAttributes = $model->attributes();
}
echo "<?php\n";
?>
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use <?= $generator->indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\widgets\\ListView" ?>;
<?= $generator->enablePjax ? 'use yii\widgets\Pjax;' : '' ?>

/* @var $this yii\web\View */
<?= !empty($generator->searchModelClass) ? "/* @var \$searchModel " . ltrim($generator->searchModelClass, '\\') . " */\n" : '' ?>
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = <?= $generator->generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>;
$this->params['breadcrumbs'][] = $this->title;
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title><?=$this->title?></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
    <div class="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index" id="<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index" style="padding: 16px;">
        <div class="layui-card">
            <div class="layui-card-body">
                <table class="layui-hide" id="dataProvider" lay-filter="dataProvider"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="toolbarProvider">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" type="button" lay-event="add">
      <i class="layui-icon layui-icon-add-1 layui-font-12"></i>
    </button>
  </div>
</script>
    <script type="text/html" id="barRowProvider">
  <div class="layui-clear-space">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><span class="layui-icon layui-icon-edit"></span></a>
    <a class="layui-btn layui-btn-xs" lay-event="more">
      <?= "<?=" ?>Yii::t('app', 'More')?>  
      <i class="layui-icon layui-icon-down"></i>
    </a>
  </div>
</script>
    <script>
        layui.use(['table', 'dropdown', 'tools'], function () {
            var table = layui.table;
            var dropdown = layui.dropdown;
            var tools = layui.tools;
            // 創建渲染實例
            var dataCols = new Array();
            dataCols.push({ type: 'checkbox', fixed: 'left' });
            <?php foreach ($generator->getColumnNames() as $attribute) {
                if (in_array($attribute, $safeAttributes)) {
                    echo "            dataCols.push({ field: '$attribute', width: 150, title: '" . $model->getAttributeLabel($attribute) . "' });\n";
                }
            } ?>
            dataCols.push({ fixed: 'right', title: '', width: 134, minWidth: 125, toolbar: '#barRowProvider' });
            table.render({
                elem: '#<?= Inflector::camel2id(StringHelper::basename($generator->modelClass)) ?>-index #dataProvider',
                url: '<?= "<?=" ?>\yii\helpers\Url::to(['index','v'=>time()])?>', // 此處為靜態模擬數據，實際使用時需換成真實接口
                toolbar: '#toolbarProvider',
                defaultToolbar: ['filter', 'exports', 'print'],
                height: 'full-35', // 最大高度減去其他容器已占有的高度差
                cellMinWidth: 80,
                totalRow: false, // 開啟合計行
                page: true,
                cols: [dataCols],
                error: function (res, msg) {
                    console.log(res, msg)
                }
            });

            // 工具欄事件
            table.on('toolbar(dataProvider)', function (obj) {
                var id = obj.config.id;
                var checkStatus = table.checkStatus(id);
                var othis = lay(this);
                switch (obj.event) {
                    case 'add':
                        layer.open({
                            title: '<?= "<?=" ?>Yii::t('app', 'Add')?>',
                            type: 1,
                            area: ['80%', '80%'],
                            content: tools.getHrefContent('<?= "<?=" ?>\yii\helpers\Url::to(['create'])?>')
                        });
                        break;
                };
            });
            
            // 觸發單元格工具事件
            table.on('tool(dataProvider)', function (obj) { // 雙擊 toolDouble
                var data = obj.data; // 獲得當前行數據
                // console.log(obj)
                if (obj.event === 'edit') {
                    layer.open({
                        title: '<?= "<?=" ?>Yii::t('app', 'Edit')?> - id:' + data.id,
                        type: 1,
                        area: ['80%', '80%'],
                        content: tools.getHrefContent('<?= "<?=" ?>\yii\helpers\Url::to(['update'])?>&id='+data.id)
                    });
                } else if (obj.event === 'more') {
                    // 更多 - 下拉菜單
                    dropdown.render({
                        elem: this, // 觸發事件的 DOM 對象
                        show: true, // 外部事件觸發即顯示
                        data: [{
                            title: '<?= "<?=" ?>Yii::t('app', 'View')?>',
                            id: 'detail'
                        }, {
                            title: '<?= "<?=" ?>Yii::t('app', 'Del')?>',
                            id: 'del'
                        }],
                        click: function (menudata) {
                            if (menudata.id === 'detail') {
                                layer.open({
                                    title: '<?= "<?=" ?>Yii::t('app', 'Detail')?> - id:' + data.id,
                                    type: 1,
                                    area: ['80%', '80%'],
                                    content: tools.getHrefContent('<?= "<?=" ?>\yii\helpers\Url::to(['view'])?>&id='+data.id)
                                });
                            } else if (menudata.id === 'del') {
                                layer.confirm('<?= "<?=" ?>Yii::t('app', 'Are you sure delete this items')?> [id: ' + data.id + ']', async function (index) {
                                    var info = await tools.syncPost('<?= "<?=" ?>\yii\helpers\Url::to(['delete'])?>&id='+data.id);
                                     if(info.message==4004){
                                        layer.msg(info.message);
                                    }else{
                                        obj.del();
                                    }
                                    layer.close(index);
                                    // 向服務端發送刪除指令
                                });
                            }
                        },
                        align: 'right', // 右對齊彈出
                        style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 設置額外樣式
                    })
                }
            });
        });
    </script>
</body>

</html>